***Deprivation in the Midst of Plenty: Citizen Polarization and Political Protest***
***John D. Griffin1* , Chad Kiewiet de Jonge and Vania Ximena Velasco-Guachalla***
***Accepted 2019***

***REPLICATION FILES***

***********************
*CREATING GRIEVANCE AND POLARIZATION MEASURES FROM SEVERAL SURVEYS ON SATISFACTION WITH DEMOCRACY*
* We created cross-nationally comparable measures of grievnace and polarization based ///
* on a common survey question that has been included on crossnational surveys ///
* across the globe over the last several decades\\\ We append all the surveys at the end, which is our polarization dataset.

*The code for the set up of each survey is below in the following order*
*1. Afrobarometer (CODE LINE 27)
*2. Asiabarometer (CODE LINE 229)
*3. Candidate Countries Eurobarometer (CODE LINE 345)
*4. Central and Eastern Eurobarometer  (CODE LINE 501)
*5. Comparative Study of Electoral Systems (CODE LINE 566)
*6. Eurobarometer (CODE LINE 840)
*7. European value Surveys (CODE LINE 1473)
*8. AmericasBarometer (CODE LINE 1788)
*9. Latinobarometro (CODE LINE 1837)
*10. New Europe Barometer (CODE LINE 1960)
*11. Post-Communist Publics Study (CODE LINE 2029)
*12. World values survey (CODE LINE 2126) 

*************************************************
*AFROBAROMETER 1ST THOUGH 4TH ROUNDS (1999-2008)*
*************************************************
*Round 1*
*Use code with afrobarometer data round 1
clear
tab dmpsat, missing

decode country, gen (Country)
tab Country

gen Year=.
replace Year=1999 if Country=="Botswana"
replace Year=1999 if Country=="Ghana"
replace Year=2000 if Country=="Lesotho"
replace Year=1999 if Country=="Malawi"
replace Year=2001 if Country=="Mali"
replace Year=1999 if Country=="Namibia"
replace Year=2000 if Country=="Nigeria"
replace Year=2000 if Country=="South Africa"
replace Year=2001 if Country=="Tanzania"
replace Year=2000 if Country=="Uganda"
replace Year=1999 if Country=="Zambia"
replace Year=1999 if Country=="Zimbabwe"

tab Year, missing

gen wt=withinwt
tab dmpsat
label list dmpsat

recode dmpsat (0=1) (2=2) (3=2.5) (4=3) (5=4) (9 97 98 99=.), gen(SWD)
keep Country Year wt SWD
*save round 1

*round 2*
*Use code with afrobarometer data round 2
tab q40, missing

decode country, gen (Country)
tab Country

gen Year=.
replace Year=2003 if Country=="Botswana"
replace Year=2002 if Country=="Cape Verde"
replace Year=2002 if Country=="Ghana"
replace Year=2003 if Country=="Kenya"
replace Year=2003 if Country=="Lesotho"
replace Year=2003 if Country=="Malawi"
replace Year=2002 if Country=="Mali"
replace Year=2002 if Country=="Mozambique"
replace Year=2003 if Country=="Namibia"
replace Year=2003 if Country=="Nigeria"
replace Year=2002 if Country=="Senegal"
replace Year=2002 if Country=="South Africa"
replace Year=2003 if Country=="Tanzania"
replace Year=2002 if Country=="Uganda"
replace Year=2003 if Country=="Zambia"
replace Year=2004 if Country=="Zimbabwe"

tab Year, missing

gen wt=withinwt
tab q40
label list q40

recode q40 (0=1)  (-1 9 98 99=.), gen(SWD)
tab SWD

keep Country Year wt SWD

*save round 2

*round 3*
*Use code with afrobarometer data round 3
tab q47, missing

decode country, gen (Country)
tab Country

gen Year=.
replace Year=2005 if Country=="Benin"
replace Year=2005 if Country=="Botswana"
replace Year=2005 if Country=="Cape Verde"
replace Year=2005 if Country=="Ghana"
replace Year=2005 if Country=="Kenya"
replace Year=2005 if Country=="Lesotho"
replace Year=2005 if Country=="Madagascar"
replace Year=2005 if Country=="Malawi"
replace Year=2005 if Country=="Mali"
replace Year=2005 if Country=="Mozambique"
replace Year=2005 if Country=="Namibia"
replace Year=2005 if Country=="Nigeria"
replace Year=2005 if Country=="Senegal"
replace Year=2006 if Country=="South Africa"
replace Year=2005 if Country=="Tanzania"
replace Year=2005 if Country=="Uganda"
replace Year=2005 if Country=="Zambia"
replace Year=2005 if Country=="Zimbabwe"

tab Year, missing

gen wt=withinwt
tab q47
label list q47

recode q47 (0=1)  (-1 9 998=.), gen(SWD)
tab SWD

keep Country Year wt SWD

*save round 3

*round 4*
*Use code with afrobarometer data round 4
tab q43, missing

decode country, gen (Country)
tab Country

gen Year=.
replace Year=2008 if Country=="Benin"
replace Year=2008 if Country=="Botswana"
replace Year=2008 if Country=="Burkina Faso"
replace Year=2008 if Country=="Cape Verde"
replace Year=2008 if Country=="Ghana"
replace Year=2008 if Country=="Kenya"
replace Year=2008 if Country=="Lesotho"
replace Year=2008 if Country=="Liberia"
replace Year=2008 if Country=="Madagascar"
replace Year=2008 if Country=="Malawi"
replace Year=2008 if Country=="Mali"
replace Year=2008 if Country=="Mozambique"
replace Year=2008 if Country=="Namibia"
replace Year=2008 if Country=="Nigeria"
replace Year=2008 if Country=="Senegal"
replace Year=2008 if Country=="South Africa"
replace Year=2008 if Country=="Tanzania"
replace Year=2008 if Country=="Uganda"
replace Year=2009 if Country=="Zambia"
replace Year=2009 if Country=="Zimbabwe"

tab Year, missing

gen wt=Withinwt
tab q43
label list q43

recode q43 (0=1)  (-1 9 998=.), gen(SWD)
tab SWD

keep Country Year wt SWD

*save round 4*
*****append all
*use r1.dta
*append using r2.dta
*append using r3.dta
*append using r4.dta

****
egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.
**************
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/66 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="Afrobarometer"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*Save as AfrobarometerData*

*************************************
*ASIANBAROMETER 1ST THOUGH 3RD WAVES*
*************************************
*Wave 1*
*Use code with asianbarometer data wave 1
decode country, gen(Country)
tab Country
replace Country="China" if Country=="Mainland China"

tab q098

gen int Year=yrsurvey

gen wt=w_all
tab q098
tab q098, nolabel missing

gen SWD=q098
replace SWD=. if q098>4
tab SWD

keep Country Year wt SWD

*save wave 1

*Waves 2 and 3*
*Use code with asianbarometer 2 & 3
decode country, gen(Country)
tab Country
replace Country="China" if Country=="Mainland China"

gen Year=.
replace Year=2007 if Country=="Japan"
replace Year=2007 if Country=="Hong Kong"
replace Year=2006 if Country=="Korea"
replace Year=2008 if Country=="China"
replace Year=2006 if Country=="Mongolia"
replace Year=2005 if Country=="Philippines"
replace Year=2006 if Country=="Taiwan"
replace Year=2006 if Country=="Thailand"
replace Year=2006 if Country=="Indonesia"
replace Year=2006 if Country=="Singapore"
replace Year=2005 if Country=="Vietnam"
replace Year=2008 if Country=="Cambodia"
replace Year=2006 if Country=="Malaysia"
tab Year, missing

gen wt=.
replace wt=w_JP if wt==.
replace wt=w_KR if wt==.
replace wt=w_CN if wt==.
replace wt=w_MN if wt==.
replace wt=w_PH if wt==.
replace wt=w_TW4 if wt==.
replace wt=w_TH if wt==.
replace wt=w_SG if wt==.
replace wt=w_KH if wt==.
replace wt=w_MY if wt==.
replace wt=1 if wt==.

tab q098

gen SWD=q098
replace SWD=. if q098>4
tab SWD

keep Country Year wt SWD

*Save wave 2
***append all

use wave1.dta
append using wave2.dta
*append using wave3.dta

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.
**************
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/21 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)


table Country, c(mean disat mean polar)
gen Study="Asianbarometer"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*Save as AsianbarometerData*

*************************************************
*CANDIDATE COUNTRIES EUROBAROMETER*
*************************************************
*Use code with candidate countries eurobarometer ZA3979
gen Year=2002
decode country, gen(Country)
tab Country

replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Czech Republic" if Country=="Czech Rep."
tab Country

gen wt=weight1
tab q49
recode q49 (8 9=.), gen(SWD)
drop if SWD==.

gen Study="Candidate Eurobarom"
gen Study_b="CC_2002.2"

keep Country Year wt SWD Study Study_b 

*save "CC Euro 2002.dta", replace
******************
clear
*use "ZA3983.dta"

tab year
gen Year=2003

decode country, gen(Country)
tab Country

replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Czech Republic" if Country=="Czech Rep."
tab Country

gen wt=weight1
tab q59
recode q59 (8 9=.), gen(SWD)
drop if SWD==.

gen Study="Candidate Eurobarom"
gen Study_b="CC_2003.2"

keep Country Year wt SWD Study Study_b 

*save "CC Euro 2003.dta", replace

******************
clear
*use "ZA3986.dta"

tab year
gen Year=2003

decode country, gen(Country)
tab Country

replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Czech Republic" if Country=="Czech Rep."
tab Country

gen wt=weight1
tab q66a
recode q66a (8 9=.), gen(SWD)
drop if SWD==.

gen Study="Candidate Eurobarom"
gen Study_b="CC_2003.4"

keep Country Year wt SWD Study Study_b 

*save "CC Euro 2003.2.dta", replace

******************
clear
*use "ZA4246.dta"

tab year
gen Year=2004

decode country, gen(Country)
tab Country

replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Czech Republic" if Country=="Czech Rep."
tab Country

gen wt=weight1
tab q39a
recode q39a (8 9=.), gen(SWD)
drop if SWD==.

gen Study="Candidate Eurobarom"
gen Study_b="CC_2004.1"

keep Country Year wt SWD Study Study_b 

*save "CC Euro 2004.dta", replace

******************
clear
*use "CC Euro 2002.dta"

**append using "CC Euro 2003.dta"
**append using "CC Euro 2003.2.dta"
**append using "CC Euro 2004.dta"

tab Year
tab Country

egen Country_Year=concat(Country Year), punct("_")
egen Country_year_n=group(Country Year)
tab Country_year_n, nolabel

egen Country_Year_Study_b=concat(Country Year Study_b), punct("_")
encode Country_Year_Study_b, gen(Country_year_Studyb_n)
tab Country_year_Studyb_n, nolabel

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/52 {
	quietly capture svy: mean SWD if Country_year_Studyb_n==`i'
	replace n_SWD=e(N) if Country_year_Studyb_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	replace disat4=m_SWD*-1+5 if Country_year_Studyb_n ==`i'
	replace disat=disat4/4 if Country_year_Studyb_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_Studyb_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_Studyb_n ==`i'
	}

	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)

collapse (first) Country_Year Country Year Study Study_b (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year_Studyb_n)

drop if m_SWD==0

*save Eurobarom SWD.dta"	

*************************************************
*CENTRAL AND EASTERN EUROBAROMETER 
*************************************************
*use code with  data  ZA3648
gen int Year=V4
decode V3, gen(Country)
tab Country

replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Russian Federation" if Country=="Russia"
drop if Country=="gdr"
tab Country

gen wt=V280
replace wt=1 if wt==.

tab V18, missing 

recode V18 (1=4) (2=3) (3=2) (4=1), gen(SWD)
tab SWD
recode V19 (1=4) (2=3) (3=2) (4=1), gen(SWD2)
replace SWD=SWD2 if SWD==.
drop SWD2
drop if SWD==.

egen Country_Year=concat(Country Year), punct("_")
egen Country_year_n=group(Country Year)
tab Country_year_n, nolabel

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/117 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="CE Eurobarometer"

collapse (first) Country_Year Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year_)

drop if m_SWD==0

* Save CE Eurobarom_trend SWD.dta

*************************************************
*COMPARATIVE STUDIES ELECTORAL SYSTEMS MODULES 1-3
*************************************************
* Use code in data module 1

decode A1006, gen(pol)

egen Country=ends(pol), punct(". ") trim tail

replace Country="Australia" if Country=="AUSTRALIA"
replace Country="Belarus" if Country=="BELARUS"
replace Country="Belgium" if Country=="BELGIUM-FLANDERS"
replace Country="Belgium" if Country=="BELGIUM-WALLOON"
replace Country="Canada" if Country=="CANADA"
replace Country="Chile" if Country=="CHILE"
replace Country="Czech Republic" if Country=="CZECH REPUBLIC"
replace Country="Denmark" if Country=="DENMARK"
replace Country="Germany" if Country=="GERMANY"
replace Country="Hong Kong" if Country=="HONG KONG SPECIAL ADMINISTRATIVE"
replace Country="Hungary" if Country=="HUNGARY"
replace Country="Iceland" if Country=="ICELAND"
replace Country="Israel" if Country=="ISRAEL"
replace Country="Japan" if Country=="JAPAN"
replace Country="Lithuania" if Country=="LITHUANIA"
replace Country="Mexico" if Country=="MEXICO"
replace Country="Netherlands" if Country=="NETHERLANDS"
replace Country="New Zealand" if Country=="NEW ZEALAND"
replace Country="Norway" if Country=="NORWAY"
replace Country="Peru" if Country=="PERU"
replace Country="Poland" if Country=="POLAND"
replace Country="Portugal" if Country=="PORTUGAL"
replace Country="Republic Of Korea" if Country=="REPUBLIC OF KOREA"
replace Country="Romania" if Country=="ROMANIA"
replace Country="Russian Federation" if Country=="RUSSIAN FEDERATION"
replace Country="Slovenia" if Country=="SLOVENIA"
replace Country="Spain" if Country=="SPAIN"
replace Country="Sweden" if Country=="SWEDEN"
replace Country="Switzerland" if Country=="SWITZERLAND"
replace Country="Taiwan " if Country=="TAIWAN PROVINCE OF CHINA"
replace Country="Thailand" if Country=="THAILAND"
replace Country="Ukraine" if Country=="UKRAINE"
replace Country="United Kingdom" if Country=="UNITED KINGDOM: GREAT BRITAIN"
replace Country="United States" if Country=="UNITED STATES"

tab Country

gen wt=A1010_3
tab A1006
tab A1006, nolabel

proportion A1006 if A1006==561 | A1006==562 [pw=wt]

gen samp_prop=.5264555 if A1006==561
replace samp_prop=.4735445 if A1006==562

gen pop_prop=.67 if A1006==561
replace pop_prop=.33 if A1006==562

gen bel_wt=pop_prop/samp_prop
proportion A1006 if A1006==561 | A1006==562 [pw=bel_wt]

replace bel_wt=1 if bel_wt==.

replace wt=wt*bel_wt

destring A1008, generate(Year)

*****setup
tab A3001
tab A3001, nolabel

recode A3001 (1=4) (2=3) (3=.) (4=2) (5=1), gen(SWD)

keep Country Year wt SWD

*save CSES1.dta
***********************

*Use module 2 data

decode B1003, gen(pol)
egen temp=ends(pol), punct(". ") trim tail
egen temp2=ends(temp), punct(" (") trim head

gen Country=temp2
tab Country

replace Country="Albania" if Country=="ALBANIA"
replace Country="Australia" if Country=="AUSTRALIA"
replace Country="Belgium" if Country=="BELGIUM"
replace Country="Brazil" if Country=="BRAZIL"
replace Country="Bulgaria" if Country=="BULGARIA"
replace Country="Canada" if Country=="CANADA"
replace Country="Chile" if Country=="CHILE"
replace Country="Czech Republic" if Country=="CZECH REPUBLIC"
replace Country="Denmark" if Country=="DENMARK"
replace Country="Finland" if Country=="FINLAND"
replace Country="France" if Country=="FRANCE"
replace Country="Germany" if Country=="GERMANY"
replace Country="Great Britain" if Country=="GREAT BRITAIN"
replace Country="Hong Kong" if Country=="HONG KONG"
replace Country="Hungary" if Country=="HUNGARY"
replace Country="Iceland" if Country=="ICELAND"
replace Country="Ireland" if Country=="IRELAND"
replace Country="Israel" if Country=="ISRAEL"
replace Country="Italy" if Country=="ITALY"
replace Country="Japan" if Country=="JAPAN"
replace Country="Korea" if Country=="KOREA"
replace Country="Kyrgyzstan" if Country=="KYRGYZSTAN"
replace Country="Mexico" if Country=="MEXICO"
replace Country="Netherlands" if Country=="NETHERLANDS"
replace Country="New Zealand" if Country=="NEW ZEALAND"
replace Country="Norway" if Country=="NORWAY"
replace Country="Peru" if Country=="PERU"
replace Country="Philippines" if Country=="PHILIPPINES"
replace Country="Poland" if Country=="POLAND"
replace Country="Portugal" if Country=="PORTUGAL"
replace Country="Romania" if Country=="ROMANIA"
replace Country="Russia" if Country=="RUSSIA"
replace Country="Slovenia" if Country=="SLOVENIA"
replace Country="Spain" if Country=="SPAIN"
replace Country="Sweden" if Country=="SWEDEN"
replace Country="Switzerland" if Country=="SWITZERLAND"
replace Country="Taiwan" if Country=="TAIWAN"
replace Country="United States" if Country=="UNITED STATES"

tab Country

gen wt=B1010_3
gen int Year=B1008
tab Year

*****setup
tab B3012
tab B3012, nolabel

recode B3012 (1=4) (2=3) (3=2) (4=1), gen(SWD)
tab SWD

keep Country Year wt SWD

*save CSES2.dta

*use module 3 data

decode C1003, gen(pol)
egen temp=ends(pol), punct(". ") trim tail
egen temp2=ends(temp), punct(" (") trim head

gen Country=temp2
tab Country

replace Country="Australia" if Country=="AUSTRALIA"
replace Country="Austria" if Country=="AUSTRIA"
replace Country="Belarus" if Country=="BELARUS"
replace Country="Brazil" if Country=="BRAZIL"
replace Country="Canada" if Country=="CANADA"
replace Country="Chile" if Country=="CHILE"
replace Country="Croatia" if Country=="CROATIA"
replace Country="Czech Republic" if Country=="CZECH REPUBLIC"
replace Country="Denmark" if Country=="DENMARK"
replace Country="Estonia" if Country=="ESTONIA"
replace Country="Finland" if Country=="FINLAND"
replace Country="France" if Country=="FRANCE"
replace Country="Germany" if Country=="GERMANY"
replace Country="Greece" if Country=="GREECE"
replace Country="Hong Kong" if Country=="HONG KONG"
replace Country="Iceland" if Country=="ICELAND"
replace Country="Ireland" if Country=="IRELAND"
replace Country="Israel" if Country=="ISRAEL"
replace Country="Japan" if Country=="JAPAN"
replace Country="Latvia" if Country=="LATVIA"
replace Country="Mexico" if Country=="MEXICO"
replace Country="Netherlands" if Country=="NETHERLANDS"
replace Country="New Zealand" if Country=="NEW ZEALAND"
replace Country="Norway" if Country=="NORWAY"
replace Country="Peru" if Country=="PERU"
replace Country="Philippines" if Country=="PHILIPPINES"
replace Country="Poland" if Country=="POLAND"
replace Country="Portugal" if Country=="PORTUGAL"
replace Country="Romania" if Country=="ROMANIA"
replace Country="Slovakia" if Country=="SLOVAKIA"
replace Country="Slovenia" if Country=="SLOVENIA"
replace Country="South Africa" if Country=="SOUTH AFRICA"
replace Country="South Korea" if Country=="SOUTH KOREA"
replace Country="Spain" if Country=="SPAIN"
replace Country="Sweden" if Country=="SWEDEN"
replace Country="Switzerland" if Country=="SWITZERLAND"
replace Country="Taiwan" if Country=="TAIWAN"
replace Country="Thailand" if Country=="THAILAND"
replace Country="Turkey" if Country=="TURKEY"
replace Country="Uruguay" if Country=="URUGUAY"
replace Country="United States" if Country=="USA"

tab Country

gen wt= C1010_3
gen int Year=C1008
tab Year

*****setup
tab C3019
tab C3019, nolabel

recode C3019 (1=4) (2=3) (4=2) (5=1) (6 7 6 8 9=.), gen(SWD)

tab SWD
keep Country Year wt SWD

* save CSES3.dta

*use CSES1.dta
*gen study_b="CSES1"
*append using CSES2.dta
*replace study_b="CSES2" if study_b==""
*append using CSES3.dta
*replace study_b="CSES3" if study_b==""

gen study="CSES"

tab Country
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="South Korea" if Country=="Republic Of Korea"
replace Country="South Korea" if Country=="Korea"
replace Country="Russian Federation" if Country=="Russia"
replace Country="Taiwan" if Country=="Taiwan "
drop if Country=="Belarus"
tab Country

drop if (Year==1999 & Country=="Chile") | (Year==2009 & Country=="Chile") | (Year==2000 & Country=="Peru")

tab Year

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

*****
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/122 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)

rename study Study
rename study_b Study_b

collapse (first) Country Year Study Study_b (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

* save CSES SWD.dta

*************************************************
*EUROBAROMETER 1970- 2002*
*************************************************
*use code with eurobarometer data  ZA3521
drop if satisdmo>4

gen int Year=year
decode nation2, gen(Country)
tab Country

replace Country="Austria" if Country=="austria"
replace Country="Belgium" if Country=="belgium"
replace Country="Denmark" if Country=="denmark"
replace Country="Finland" if Country=="finland"
replace Country="France" if Country=="france"
replace Country="Germany" if Country=="germany"
replace Country="Greece" if Country=="greece"
replace Country="Ireland" if Country=="ireland"
replace Country="Italy" if Country=="italy"
replace Country="Luxembourg" if Country=="luxembourg"
replace Country="Netherlands" if Country=="netherlands"
replace Country="Norway" if Country=="norway"
replace Country="Portugal" if Country=="portugal"
replace Country="Spain" if Country=="spain"
replace Country="Sweden" if Country=="sweden"
replace Country="United Kingdom" if Country=="uk"
tab Country

gen wt=wnation
replace wt=1 if wt==.

tab satisdmo, missing 

recode satisdmo (1=4) (2=3) (3=2) (4=1), gen(SWD)
tab SWD

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

decode eb, gen(Study_b)
egen Country_Year_Study_b=concat(Country Year Study_b), punct("_")
encode Country_Year_Study_b, gen(Country_year_Studyb_n)
tab Country_year_Studyb_n, nolabel

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/588 {
	quietly capture svy: mean SWD if Country_year_Studyb_n==`i'
	replace n_SWD=e(N) if Country_year_Studyb_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	replace disat4=m_SWD*-1+5 if Country_year_Studyb_n ==`i'
	replace disat=disat4/4 if Country_year_Studyb_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_Studyb_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_Studyb_n ==`i'
	}
	
table Country_year_Studyb, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="Eurobarometer"

collapse (first) Country_Year Country Year Study Study_b (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year_Studyb)

*save eurobarometer1970-2002

*************************************************
*EUROBAROMETER POST - 2002*
*************************************************
*Round 1*
*Use code with eurobarometer ZA3904
gen Year=2003

decode v13, gen(Country)
replace Country="Germany" if Country=="Germany (West+East)"
replace Country="Netherlands" if Country=="The Netherlands"
tab Country

gen wt=v14

lookfor satisfaction

tab v161
recode v161 (1=4) (2=3) (3=2) (4=1), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d

tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save Euro59

*Use code with eurobarometer ZA3938

gen Year=2003

decode v12, gen(Country)
replace Country="Germany" if Country=="Germany (West+East)"
replace Country="Netherlands" if Country=="The Netherlands"
tab Country

gen wt=v13

lookfor satisfaction

tab v152
recode v152 (1=4) (2=3) (3=2) (4=1), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro60

*Use code with eurobarometer ZA4231
tab v4

gen Year=2004

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"

tab Country

gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v231
recode v231 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro62

*Use code with eurobarometer ZA4411
tab v4

gen Year=2005

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"

tab Country
gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v257
recode v257 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro63

*Use code with eurobarometer ZA4506
tab v4

gen Year=2006

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"

tab Country
gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v3104
recode v3104 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro65

*Use code with eurobarometer ZA4565
tab v4
lookfor satisfaction
lookfor democracy

gen Year=2007

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Macedonia" if Country=="Macedonia (FYROM)"

tab Country

gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v173
recode v173 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro68

*Use code with eurobarometer ZA4994
tab v4
lookfor satisfaction
lookfor democracy

gen Year=2009

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Macedonia" if Country=="Macedonia (FYROM)"

tab Country

gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v271
recode v271 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

* save euro72

*Use code with eurobarometer ZA5234
tab v4
lookfor satisfaction
lookfor democracy

gen Year=2010

decode v6, gen(Country)
tab Country
replace Country="Germany" if Country=="Germany East"
replace Country="Germany" if Country=="Germany West"
replace Country="Netherlands" if Country=="The Netherlands"
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="United Kingdom" if Country=="Northern Ireland"
replace Country="Cyprus" if Country=="Cyprus (Republic)"
replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Macedonia" if Country=="Macedonia (FYROM)"

tab Country

gen wt=v8
replace wt=v10 if v10>0
replace wt=v12 if v12>0

lookfor satisfaction

tab v359
recode v359 (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode v4, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro73

*Use code with eurobarometer ZA5567
tab survey
lookfor satisfaction
lookfor democracy

gen Year=2011

gen Country=""

replace Country="France" if country==1
replace Country="Belgium" if country==2
replace Country="Netherlands" if country==3
replace Country="Germany" if country==4
replace Country="Italy" if country==5
replace Country="Luxembourg" if country==6
replace Country="Denmark" if country==7
replace Country="Ireland" if country==8
replace Country="United Kingdom" if country==9
replace Country="United Kingdom" if country==10
replace Country="Greece" if country==11
replace Country="Spain" if country==12
replace Country="Portugal" if country==13
replace Country="Germany" if country==14
replace Country="Finland" if country==16
replace Country="Sweden" if country==17
replace Country="Austria" if country==18
replace Country="Cyprus" if country==19
replace Country="Czech Republic" if country==20
replace Country="Estonia" if country==21
replace Country="Hungary" if country==22
replace Country="Latvia" if country==23
replace Country="Lithuania" if country==24
replace Country="Malta" if country==25
replace Country="Poland" if country==26
replace Country="Slovak Republic" if country==27
replace Country="Slovenia" if country==28
replace Country="Bulgaria" if country==29
replace Country="Romania" if country==30
replace Country="Turkey" if country==31
replace Country="Croatia" if country==32
replace Country="Cyprus (TCC)" if country==33
replace Country="Macedonia" if country==34
replace Country="Montenegro" if country==35
replace Country="Iceland" if country==43

tab Country

gen wt=w1
replace wt=w3 if w3>0
replace wt=w4 if w4>0

lookfor satisfaction

tab qa18a
recode qa18a (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode survey, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro76

*Use code with eurobarometer ZA5612
tab survey
lookfor satisfaction
lookfor democracy

gen Year=2012
tab country
gen Country=""

replace Country="France" if country==1
replace Country="Belgium" if country==2
replace Country="Netherlands" if country==3
replace Country="Germany" if country==4
replace Country="Italy" if country==5
replace Country="Luxembourg" if country==6
replace Country="Denmark" if country==7
replace Country="Ireland" if country==8
replace Country="United Kingdom" if country==9
replace Country="United Kingdom" if country==10
replace Country="Greece" if country==11
replace Country="Spain" if country==12
replace Country="Portugal" if country==13
replace Country="Germany" if country==14
replace Country="Finland" if country==16
replace Country="Sweden" if country==17
replace Country="Austria" if country==18
replace Country="Cyprus" if country==19
replace Country="Czech Republic" if country==20
replace Country="Estonia" if country==21
replace Country="Hungary" if country==22
replace Country="Latvia" if country==23
replace Country="Lithuania" if country==24
replace Country="Malta" if country==25
replace Country="Poland" if country==26
replace Country="Slovak Republic" if country==27
replace Country="Slovenia" if country==28
replace Country="Bulgaria" if country==29
replace Country="Romania" if country==30
replace Country="Turkey" if country==31
replace Country="Croatia" if country==32
replace Country="Cyprus (TCC)" if country==33
replace Country="Macedonia" if country==34
replace Country="Montenegro" if country==35
replace Country="Serbia" if country==36
replace Country="Iceland" if country==43

tab Country

gen wt=w1
replace wt=w3 if w3>0
replace wt=w4 if w4>0

lookfor satisfaction

tab qa20a
recode qa20a (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode survey, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro77

*Use code with eurobarometer ZA5685
tab SURVEY
lookfor satisfaction
lookfor democracy

gen Year=2012
tab COUNTRY
rename COUNTRY country
gen Country=""

replace Country="France" if country==1
replace Country="Belgium" if country==2
replace Country="Netherlands" if country==3
replace Country="Germany" if country==4
replace Country="Italy" if country==5
replace Country="Luxembourg" if country==6
replace Country="Denmark" if country==7
replace Country="Ireland" if country==8
replace Country="United Kingdom" if country==9
replace Country="United Kingdom" if country==10
replace Country="Greece" if country==11
replace Country="Spain" if country==12
replace Country="Portugal" if country==13
replace Country="Germany" if country==14
replace Country="Finland" if country==16
replace Country="Sweden" if country==17
replace Country="Austria" if country==18
replace Country="Cyprus" if country==19
replace Country="Czech Republic" if country==20
replace Country="Estonia" if country==21
replace Country="Hungary" if country==22
replace Country="Latvia" if country==23
replace Country="Lithuania" if country==24
replace Country="Malta" if country==25
replace Country="Poland" if country==26
replace Country="Slovak Republic" if country==27
replace Country="Slovenia" if country==28
replace Country="Bulgaria" if country==29
replace Country="Romania" if country==30
replace Country="Turkey" if country==31
replace Country="Croatia" if country==32
replace Country="Cyprus (TCC)" if country==33
replace Country="Macedonia" if country==34
replace Country="Montenegro" if country==35
replace Country="Serbia" if country==36
replace Country="Iceland" if country==43

tab Country

gen wt=w1
replace wt=w3 if w3>0
replace wt=w4 if w4>0

lookfor satisfaction

tab qa19a 
recode qa19a  (1=4) (2=3) (3=2) (4=1) (5=.), gen(SWD)
drop if SWD==.
drop if SWD==.i
drop if SWD==.d
tab SWD

gen Study="Eurobarometer"
decode SURVEY, gen(Study_b)

keep Country Year wt SWD Study Study_b 

*save euro78

************************
*Append 

*use "Euro 59.1.dta"

*append using "Euro 60.1.dta"
*append using "Euro 62.2.dta"
*append using "Euro 63.4.dta"
*append using "Euro 65.2.dta"
*append using "Euro 68.1.dta"
*append using "Euro 72.4.dta"
*append using "Euro 73.4.dta"
*append using "Euro 76.3.dta"
*append using "Euro 77.3.dta"
*append using "Euro 78.1.dta"

tab Country
replace Country="Cyprus (TCC)" if Country=="Cyprus (CY-TCC)"
tab Country

egen Country_Year=concat(Country Year), punct("_")
egen Country_year_n=group(Country Year)
tab Country_year_n, nolabel

egen Country_Year_Study_b=concat(Country Year Study_b), punct("_")
encode Country_Year_Study_b, gen(Country_year_Studyb_n)
tab Country_year_Studyb_n, nolabel

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/288 {
	clear matrix
	quietly capture svy: mean SWD if Country_year_Studyb_n==`i'
	replace n_SWD=e(N) if Country_year_Studyb_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_Studyb_n==`i'
	replace disat4=m_SWD*-1+5 if Country_year_Studyb_n ==`i'
	replace disat=disat4/4 if Country_year_Studyb_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_Studyb_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_Studyb_n ==`i'
	}

	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)

collapse (first) Country_Year Country Year Study Study_b (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year_Studyb_n)

*Save eurobarometerdatapost2002

*************************************************
*EUROPAN VALUES SURVEY 1981-2008
*************************************************
*Use code with european values survey data ZA4804

drop if e110>4

label list s003

gen Country=""
replace Country="Afghanistan" if s003==4
replace Country="Albania" if s003==8
replace Country="Algeria" if s003==12
replace Country="American Samoa" if s003==16
replace Country="Andorra" if s003==20
replace Country="Angola" if s003==24
replace Country="Antigua and Barbuda" if s003==28
replace Country="Azerbaijan" if s003==31
replace Country="Argentina" if s003==32
replace Country="Australia" if s003==36
replace Country="Austria" if s003==40
replace Country="Bahamas" if s003==44
replace Country="Bahrain" if s003==48
replace Country="Bangladesh" if s003==50
replace Country="Armenia" if s003==51
replace Country="Barbados" if s003==52
replace Country="Belgium" if s003==56
replace Country="Bermuda" if s003==60
replace Country="Bhutan" if s003==64
replace Country="Bolivia" if s003==68
replace Country="Bosnia and Herzegovina" if s003==70
replace Country="Botswana" if s003==72
replace Country="Brazil" if s003==76
replace Country="Belize" if s003==84
replace Country="Solomon Islands" if s003==90
replace Country="British Virgin Islands" if s003==92
replace Country="Brunei Darussalam" if s003==96
replace Country="Bulgaria" if s003==100
replace Country="Myanmar" if s003==104
replace Country="Burundi" if s003==108
replace Country="Belarus" if s003==112
replace Country="Cambodia" if s003==116
replace Country="Cameroon" if s003==120
replace Country="Canada" if s003==124
replace Country="Cape Verde" if s003==132
replace Country="Cayman Islands" if s003==136
replace Country="Central African Republic" if s003==140
replace Country="Sri Lanka" if s003==144
replace Country="Chad" if s003==148
replace Country="Chile" if s003==152
replace Country="China" if s003==156
replace Country="Taiwan" if s003==158
replace Country="Colombia" if s003==170
replace Country="Comoros" if s003==174
replace Country="Mayotte" if s003==175
replace Country="Congo" if s003==178
replace Country="Democratic Republic of the Congo" if s003==180
replace Country="Cook Islands" if s003==184
replace Country="Costa Rica" if s003==188
replace Country="Croatia" if s003==191
replace Country="Cuba" if s003==192
replace Country="Cyprus" if s003==196
replace Country="Czech Republic" if s003==203
replace Country="Benin" if s003==204
replace Country="Denmark" if s003==208
replace Country="Dominica" if s003==212
replace Country="Dominican Republic" if s003==214
replace Country="Ecuador" if s003==218
replace Country="El Salvador" if s003==222
replace Country="Equatorial Guinea" if s003==226
replace Country="Ethiopia" if s003==231
replace Country="Eritrea" if s003==232
replace Country="Estonia" if s003==233
replace Country="Faeroe Islands" if s003==234
replace Country="Falkland Islands (Malvinas)" if s003==238
replace Country="Fiji" if s003==242
replace Country="Finland" if s003==246
replace Country="France" if s003==250
replace Country="French Guiana" if s003==254
replace Country="French Polynesia" if s003==258
replace Country="Djibouti" if s003==262
replace Country="Gabon" if s003==266
replace Country="Georgia" if s003==268
replace Country="Gambia" if s003==270
replace Country="Occupied Palestinian Territory" if s003==275
replace Country="Germany" if s003==276
replace Country="Ghana" if s003==288
replace Country="Gibraltar" if s003==292
replace Country="Kiribati" if s003==296
replace Country="Greece" if s003==300
replace Country="Greenland" if s003==304
replace Country="Grenada" if s003==308
replace Country="Guadeloupe" if s003==312
replace Country="Guam" if s003==316
replace Country="Guatemala" if s003==320
replace Country="Guinea" if s003==324
replace Country="Guyana" if s003==328
replace Country="Haiti" if s003==332
replace Country="Holy See" if s003==336
replace Country="Honduras" if s003==340
replace Country="Hong Kong" if s003==344
replace Country="Hungary" if s003==348
replace Country="Iceland" if s003==352
replace Country="India" if s003==356
replace Country="Indonesia" if s003==360
replace Country="Iran" if s003==364
replace Country="Iraq" if s003==368
replace Country="Ireland" if s003==372
replace Country="Israel" if s003==376
replace Country="Italy" if s003==380
replace Country="Côte d´Ivoire" if s003==384
replace Country="Jamaica" if s003==388
replace Country="Japan" if s003==392
replace Country="Kazakhstan" if s003==398
replace Country="Jordan" if s003==400
replace Country="Kenya" if s003==404
replace Country="Democratic People´s Republic of Korea" if s003==408
replace Country="South Korea" if s003==410
replace Country="Kuwait" if s003==414
replace Country="Kyrgyzstan" if s003==417
replace Country="Lao People´s Democratic Republic" if s003==418
replace Country="Lebanon" if s003==422
replace Country="Lesotho" if s003==426
replace Country="Latvia" if s003==428
replace Country="Liberia" if s003==430
replace Country="Libyan Arab Jamahiriya" if s003==434
replace Country="Liechtenstein" if s003==438
replace Country="Lithuania" if s003==440
replace Country="Luxembourg" if s003==442
replace Country="Macao Special Administrative Region of China" if s003==446
replace Country="Madagascar" if s003==450
replace Country="Malawi" if s003==454
replace Country="Malaysia" if s003==458
replace Country="Maldives" if s003==462
replace Country="Mali" if s003==466
replace Country="Malta" if s003==470
replace Country="Martinique" if s003==474
replace Country="Mauritania" if s003==478
replace Country="Mauritius" if s003==480
replace Country="Mexico" if s003==484
replace Country="Monaco" if s003==492
replace Country="Mongolia" if s003==496
replace Country="Moldova" if s003==498
replace Country="Montserrat" if s003==500
replace Country="Morocco" if s003==504
replace Country="Mozambique" if s003==508
replace Country="Oman" if s003==512
replace Country="Namibia" if s003==516
replace Country="Nauru" if s003==520
replace Country="Nepal" if s003==524
replace Country="Netherlands" if s003==528
replace Country="Netherlands Antilles" if s003==530
replace Country="Aruba" if s003==533
replace Country="New Caledonia" if s003==540
replace Country="Vanuatu" if s003==548
replace Country="New Zealand" if s003==554
replace Country="Nicaragua" if s003==558
replace Country="Niger" if s003==562
replace Country="Nigeria" if s003==566
replace Country="Niue" if s003==570
replace Country="Norfolk Island" if s003==574
replace Country="Norway" if s003==578
replace Country="Northern Mariana Islands" if s003==580
replace Country="Micronesia" if s003==583
replace Country="Marshall Islands" if s003==584
replace Country="Palau" if s003==585
replace Country="Pakistan" if s003==586
replace Country="Panama" if s003==591
replace Country="Papua New Guinea" if s003==598
replace Country="Paraguay" if s003==600
replace Country="Peru" if s003==604
replace Country="Philippines" if s003==608
replace Country="Pitcairn" if s003==612
replace Country="Poland" if s003==616
replace Country="Portugal" if s003==620
replace Country="Guinea-Bissau" if s003==624
replace Country="Timor-Leste" if s003==626
replace Country="Puerto Rico" if s003==630
replace Country="Qatar" if s003==634
replace Country="Réunion" if s003==638
replace Country="Romania" if s003==642
replace Country="Russian Federation" if s003==643
replace Country="Rwanda" if s003==646
replace Country="Saint Helena" if s003==654
replace Country="Saint Kitts and Nevis" if s003==659
replace Country="Anguilla" if s003==660
replace Country="Saint Lucia" if s003==662
replace Country="Saint Pierre and Miquelon" if s003==666
replace Country="Saint Vincent and the Grenadines" if s003==670
replace Country="San Marino" if s003==674
replace Country="Sao Tome and Principe" if s003==678
replace Country="Saudi Arabia" if s003==682
replace Country="Senegal" if s003==686
replace Country="Seychelles" if s003==690
replace Country="Sierra Leone" if s003==694
replace Country="Singapore" if s003==702
replace Country="Slovakia" if s003==703
replace Country="Vietnam" if s003==704
replace Country="Slovenia" if s003==705
replace Country="Somalia" if s003==706
replace Country="South Africa" if s003==710
replace Country="Zimbabwe" if s003==716
replace Country="Spain" if s003==724
replace Country="Western Sahara" if s003==732
replace Country="Sudan" if s003==736
replace Country="Suriname" if s003==740
replace Country="Svalbard and Jan Mayen Islands" if s003==744
replace Country="Swaziland" if s003==748
replace Country="Sweden" if s003==752
replace Country="Switzerland" if s003==756
replace Country="Syrian Arab Republic" if s003==760
replace Country="Tajikistan" if s003==762
replace Country="Thailand" if s003==764
replace Country="Togo" if s003==768
replace Country="Tokelau" if s003==772
replace Country="Tonga" if s003==776
replace Country="Trinidad and Tobago" if s003==780
replace Country="United Arab Emirates" if s003==784
replace Country="Tunisia" if s003==788
replace Country="Turkey" if s003==792
replace Country="Turkmenistan" if s003==795
replace Country="Turks and Caicos Islands" if s003==796
replace Country="Tuvalu" if s003==798
replace Country="Uganda" if s003==800
replace Country="Ukraine" if s003==804
replace Country="Macedonia" if s003==807
replace Country="Egypt" if s003==818
replace Country="Great Britain" if s003==826
replace Country="Isle of Man" if s003==833
replace Country="Tanzania" if s003==834
replace Country="United States" if s003==840
replace Country="United States Virgin Islands" if s003==850
replace Country="Burkina Faso" if s003==854
replace Country="Uruguay" if s003==858
replace Country="Uzbekistan" if s003==860
replace Country="Venezuela" if s003==862
replace Country="Wallis and Futuna Islands" if s003==876
replace Country="Samoa" if s003==882
replace Country="Yemen" if s003==887
replace Country="Serbia and Montenegro" if s003==891
replace Country="Zambia" if s003==894
replace Country="Germany West" if s003==900
replace Country="Germany East" if s003==901
replace Country="Tambov" if s003==902
replace Country="Moscow" if s003==903
replace Country="Basque Country" if s003==904
replace Country="Andalusia" if s003==906
replace Country="Galicia" if s003==907
replace Country="Northern Ireland" if s003==909
replace Country="Valencia" if s003==910
replace Country="Serbia" if s003==911
replace Country="Montenegro" if s003==912
replace Country="SrpSka - Serbian Republic of Bosnia" if s003==913
replace Country="Bosnia Federation" if s003==914

tab Country
tab s003 if Country==""
tab s003 if Country=="", nolabel

replace Country="Northern Cyprus" if s003==197
replace Country="Montenegro" if s003==499
replace Country="Serbia" if s003==688
replace Country="Kosovo" if s003==915
tab Country


gen int Year=s020
tab Year
gen wt=s017

tab e110
recode e110 (1=4) (2=3) (3=2) (4=1), gen(SWD)

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.

**************
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/80 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}

	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="EVS"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*Save europeanEVS	

*************************************************
*LATIN AMERICAN PUBLIC OPINION PROJECT 2004-2012
*************************************************
*Use code with Lapop merged data 2004-2012

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)

replace wt=1 if wt==.

tab pn4

recode pn4 (1=4) (2=3) (3=2) (4=1) (999999 .a .b .c=.), gen(SWD)

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/107 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
	
table Country_Year, c(mean n_SWD mean m_SWD mean sd_SWD)
table Country, c(mean n_SWD mean m_SWD mean sd_SWD)

table Country, c(mean n_SWD mean disat mean polar)
gen Study="LAPOP"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*save lapop

*************************************************
*LATINOBAROMETER 1996-2008
*************************************************
*Use code with Latinobarometer data 2008
decode idenpa, generate(Country)

gen Year=2008

recode p22st_a (0 8=.) (1=4) (2=3) (3=2) (4=1), gen(dem_satis)

keep Country Year wt dem_satis idenpa
tab dem_satis

*save Latinobarometer2008

*Use code with Latinobarometer data 2009
decode idenpa, generate(Country)

gen Year=2009

recode p12st_a (0 8=.) (1=4) (2=3) (3=2) (4=1), gen(dem_satis)

keep Country Year wt dem_satis idenpa
tab dem_satis

*save Latinobarometer2009

*Use code with Latinobarometer data 2010
decode idenpa, generate(Country)

gen Year=2010

recode P11ST_A (0 8=.) (1=4) (2=3) (3=2) (4=1), gen(dem_satis)

keep Country Year wt dem_satis idenpa
tab dem_satis

*save Latinobarometer2010

*Use code with data Latinobarometer 1996-2007

rename Country Country_ISO

gen Country=""
replace Country="Argentina" if Country_ISO ==32
replace Country ="Bolivia" if Country_ISO ==68
replace Country ="Brazil" if Country_ISO ==76
replace Country ="Chile" if Country_ISO ==152
replace Country ="Colombia" if Country_ISO ==170
replace Country ="Costa Rica" if Country_ISO ==188
replace Country ="Dominican Republic" if Country_ISO ==214
replace Country ="Ecuador" if Country_ISO ==218
replace Country ="El Salvador" if Country_ISO ==222
replace Country ="Guatemala" if Country_ISO ==320
replace Country ="Honduras" if Country_ISO ==340
replace Country ="Mexico" if Country_ISO ==484
replace Country ="Nicaragua" if Country_ISO ==558
replace Country ="Panama" if Country_ISO ==591
replace Country ="Paraguay" if Country_ISO ==600
replace Country ="Peru" if Country_ISO ==604
replace Country ="Spain" if Country_ISO ==724
replace Country ="Uruguay" if Country_ISO ==858
replace Country ="Venezuela" if Country_ISO ==862

*append using "LB 2008.dta"
*append using "LB 2009.dta"
*append using "LB 2010.dta"

tab Country

replace Country="Brazil" if Country=="Brasil"
replace Country="Spain" if Country=="Espa�a"
replace Country="Mexico" if Country=="M�xico"
replace Country="Panama" if Country=="Panam�"
replace Country="Peru" if Country=="Per�"
replace Country="Dominican Republic" if Country=="Rep�blica Dominicana"

tab Country

keep Country Year dem_satis wt
*****
egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.

gen SWD=dem_satis

gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/258 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="Latinobarometro"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*save Latinobarometerfinal

*************************************************
*NEW EUROPE BAROMETER
*************************************************
* Use new eurobarometer data
drop year

drop if a2==.

gen Country=ncountr
replace Country=13 if country=="Scroat"
replace Country=14 if country=="Tbelor"
label values Country ncountr

label define ncountr 2"Czech Republic" 3 "Slovakia" 13 "Croatia" 14 "Belarus", modify add

rename Country country_num

decode country_num, gen(Country)
replace Country="Serbia" if Country==""
tab Country

gen wt=w
gen int Year=year_fix

tab a2
tab a2, nolabel

recode a2 (1=4) (2=3) (3=2) (4=1) (98=.), gen(SWD)

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.
**************
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/25 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="New Euro Barom"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

* save NewEuro.dta	

*************************************************
*POST COMMUNIST PUBLICS STUDY
*************************************************
* use  Post-Communist Publics DATA ZA4054

gen int Year=.
replace Year=1990 if V666==1
replace Year=1990 if V666==2
replace Year=1990 if V666==3
replace Year=1991 if V666==4
replace Year=1991 if V666==5
replace Year=1991 if V666==6
replace Year=1992 if V666==7
replace Year=1998 if V666==8
replace Year=1998 if V666==9
replace Year=1998 if V666==10
replace Year=1998 if V666==11
replace Year=1998 if V666==12
replace Year=1998 if V666==13
replace Year=1998 if V666==14
replace Year=1999 if V666==15
replace Year=1999 if V666==16
replace Year=1999 if V666==17
replace Year=1999 if V666==18
replace Year=1999 if V666==19
replace Year=2000 if V666==20
replace Year=2000 if V666==21
replace Year=2001 if V666==22
replace Year=2001 if V666==23
replace Year=2001 if V666==24
replace Year=2001 if V666==25

decode V3, gen(Country)
tab Country

drop if Country=="Krasnoyarsk"
replace Country="Slovak Republic" if Country=="Slovakia"
replace Country="Russian Federation" if Country=="Russia" 
replace Country="Germany" if Country=="West Germany"
replace Country="Germany" if Country=="East Germany"

tab Country
gen wt=.
replace wt=V633 if wt==.
replace wt=V634 if wt==.
replace wt=V635 if wt==.
replace wt=V636 if wt==.
replace wt=V637 if wt==.
replace wt=V638 if wt==.
replace wt=V639 if wt==.
replace wt=V640 if wt==.
replace wt=1 if wt==.

gen SWD10=V173
drop if SWD10==.

egen Country_Year=concat(Country Year), punct("_")
egen Country_year_n=group(Country Year)
tab Country_year_n, nolabel

gen m_SWD10=.
gen sd_SWD10=.
gen n_SWD10=.
gen disat=.
gen disat10=.
gen maxsd10=.
gen polar10=.

svyset [pw=wt]

forvalues i=1/25 {
	quietly capture svy: mean SWD10 if Country_year_n ==`i'
	replace n_SWD10=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD10=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat10=m_SWD*-1+11 if Country_year_n ==`i'
	replace disat=disat10/10 if Country_year_n ==`i'
	replace maxsd10=sqrt((1-m_SWD10)*(m_SWD10-10)*(n_SWD10)/(n_SWD10-1)) if Country_year_n ==`i'
	replace polar=sd_SWD10/maxsd10 if Country_year_n ==`i'
	}
	

table Country_year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="Post-Communist Publics"

collapse (first) Country_Year Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year_)

drop if m_SWD==.

* save Post-Communist Publics SWD.dta

*************************************************
*WORLD VALUES SURVEY 1981-2008
*************************************************
*Use data with world values survey merge 1981-2008
drop if E110==.

label list S003

gen Country=""

replace Country="Afghanistan" if S003==4
replace Country="Albania" if S003==8
replace Country="Algeria" if S003==12
replace Country="American Samoa" if S003==16
replace Country="Andorra" if S003==20
replace Country="Angola" if S003==24
replace Country="Antigua and Barbuda" if S003==28
replace Country="Azerbaijan" if S003==31
replace Country="Argentina" if S003==32
replace Country="Australia" if S003==36
replace Country="Austria" if S003==40
replace Country="Bahamas" if S003==44
replace Country="Bahrain" if S003==48
replace Country="Bangladesh" if S003==50
replace Country="Armenia" if S003==51
replace Country="Barbados" if S003==52
replace Country="Belgium" if S003==56
replace Country="Bermuda" if S003==60
replace Country="Bhutan" if S003==64
replace Country="Bolivia" if S003==68
replace Country="Bosnia and Herzegovina" if S003==70
replace Country="Botswana" if S003==72
replace Country="Brazil" if S003==76
replace Country="Belize" if S003==84
replace Country="Solomon Islands" if S003==90
replace Country="British Virgin Islands" if S003==92
replace Country="Brunei Darussalam" if S003==96
replace Country="Bulgaria" if S003==100
replace Country="Myanmar" if S003==104
replace Country="Burundi" if S003==108
replace Country="Belarus" if S003==112
replace Country="Cambodia" if S003==116
replace Country="Cameroon" if S003==120
replace Country="Canada" if S003==124
replace Country="Cape Verde" if S003==132
replace Country="Cayman Islands" if S003==136
replace Country="Central African Republic" if S003==140
replace Country="Sri Lanka" if S003==144
replace Country="Chad" if S003==148
replace Country="Chile" if S003==152
replace Country="China" if S003==156
replace Country="Taiwan" if S003==158
replace Country="Colombia" if S003==170
replace Country="Comoros" if S003==174
replace Country="Mayotte" if S003==175
replace Country="Congo" if S003==178
replace Country="Democratic Republic of the Congo" if S003==180
replace Country="Cook Islands" if S003==184
replace Country="Costa Rica" if S003==188
replace Country="Croatia" if S003==191
replace Country="Cuba" if S003==192
replace Country="Cyprus" if S003==196
replace Country="Czech Republic" if S003==203
replace Country="Benin" if S003==204
replace Country="Denmark" if S003==208
replace Country="Dominica" if S003==212
replace Country="Dominican Republic" if S003==214
replace Country="Ecuador" if S003==218
replace Country="El Salvador" if S003==222
replace Country="Equatorial Guinea" if S003==226
replace Country="Ethiopia" if S003==231
replace Country="Eritrea" if S003==232
replace Country="Estonia" if S003==233
replace Country="Faeroe Islands" if S003==234
replace Country="Falkland Islands (Malvinas)" if S003==238
replace Country="Fiji" if S003==242
replace Country="Finland" if S003==246
replace Country="France" if S003==250
replace Country="French Guiana" if S003==254
replace Country="French Polynesia" if S003==258
replace Country="Djibouti" if S003==262
replace Country="Gabon" if S003==266
replace Country="Georgia" if S003==268
replace Country="Gambia" if S003==270
replace Country="Occupied Palestinian Territory" if S003==275
replace Country="Germany" if S003==276
replace Country="Ghana" if S003==288
replace Country="Gibraltar" if S003==292
replace Country="Kiribati" if S003==296
replace Country="Greece" if S003==300
replace Country="Greenland" if S003==304
replace Country="Grenada" if S003==308
replace Country="Guadeloupe" if S003==312
replace Country="Guam" if S003==316
replace Country="Guatemala" if S003==320
replace Country="Guinea" if S003==324
replace Country="Guyana" if S003==328
replace Country="Haiti" if S003==332
replace Country="Holy See" if S003==336
replace Country="Honduras" if S003==340
replace Country="Hong Kong" if S003==344
replace Country="Hungary" if S003==348
replace Country="Iceland" if S003==352
replace Country="India" if S003==356
replace Country="Indonesia" if S003==360
replace Country="Iran" if S003==364
replace Country="Iraq" if S003==368
replace Country="Ireland" if S003==372
replace Country="Israel" if S003==376
replace Country="Italy" if S003==380
replace Country="Côte d´Ivoire" if S003==384
replace Country="Jamaica" if S003==388
replace Country="Japan" if S003==392
replace Country="Kazakhstan" if S003==398
replace Country="Jordan" if S003==400
replace Country="Kenya" if S003==404
replace Country="Democratic People´s Republic of Korea" if S003==408
replace Country="South Korea" if S003==410
replace Country="Kuwait" if S003==414
replace Country="Kyrgyzstan" if S003==417
replace Country="Lao People´s Democratic Republic" if S003==418
replace Country="Lebanon" if S003==422
replace Country="Lesotho" if S003==426
replace Country="Latvia" if S003==428
replace Country="Liberia" if S003==430
replace Country="Libyan Arab Jamahiriya" if S003==434
replace Country="Liechtenstein" if S003==438
replace Country="Lithuania" if S003==440
replace Country="Luxembourg" if S003==442
replace Country="Macao Special Administrative Region of China" if S003==446
replace Country="Madagascar" if S003==450
replace Country="Malawi" if S003==454
replace Country="Malaysia" if S003==458
replace Country="Maldives" if S003==462
replace Country="Mali" if S003==466
replace Country="Malta" if S003==470
replace Country="Martinique" if S003==474
replace Country="Mauritania" if S003==478
replace Country="Mauritius" if S003==480
replace Country="Mexico" if S003==484
replace Country="Monaco" if S003==492
replace Country="Mongolia" if S003==496
replace Country="Moldova" if S003==498
replace Country="Montserrat" if S003==500
replace Country="Morocco" if S003==504
replace Country="Mozambique" if S003==508
replace Country="Oman" if S003==512
replace Country="Namibia" if S003==516
replace Country="Nauru" if S003==520
replace Country="Nepal" if S003==524
replace Country="Netherlands" if S003==528
replace Country="Netherlands Antilles" if S003==530
replace Country="Aruba" if S003==533
replace Country="New Caledonia" if S003==540
replace Country="Vanuatu" if S003==548
replace Country="New Zealand" if S003==554
replace Country="Nicaragua" if S003==558
replace Country="Niger" if S003==562
replace Country="Nigeria" if S003==566
replace Country="Niue" if S003==570
replace Country="Norfolk Island" if S003==574
replace Country="Norway" if S003==578
replace Country="Northern Mariana Islands" if S003==580
replace Country="Micronesia" if S003==583
replace Country="Marshall Islands" if S003==584
replace Country="Palau" if S003==585
replace Country="Pakistan" if S003==586
replace Country="Panama" if S003==591
replace Country="Papua New Guinea" if S003==598
replace Country="Paraguay" if S003==600
replace Country="Peru" if S003==604
replace Country="Philippines" if S003==608
replace Country="Pitcairn" if S003==612
replace Country="Poland" if S003==616
replace Country="Portugal" if S003==620
replace Country="Guinea-Bissau" if S003==624
replace Country="Timor-Leste" if S003==626
replace Country="Puerto Rico" if S003==630
replace Country="Qatar" if S003==634
replace Country="Réunion" if S003==638
replace Country="Romania" if S003==642
replace Country="Russian Federation" if S003==643
replace Country="Rwanda" if S003==646
replace Country="Saint Helena" if S003==654
replace Country="Saint Kitts and Nevis" if S003==659
replace Country="Anguilla" if S003==660
replace Country="Saint Lucia" if S003==662
replace Country="Saint Pierre and Miquelon" if S003==666
replace Country="Saint Vincent and the Grenadines" if S003==670
replace Country="San Marino" if S003==674
replace Country="Sao Tome and Principe" if S003==678
replace Country="Saudi Arabia" if S003==682
replace Country="Senegal" if S003==686
replace Country="Seychelles" if S003==690
replace Country="Sierra Leone" if S003==694
replace Country="Singapore" if S003==702
replace Country="Slovakia" if S003==703
replace Country="Vietnam" if S003==704
replace Country="Slovenia" if S003==705
replace Country="Somalia" if S003==706
replace Country="South Africa" if S003==710
replace Country="Zimbabwe" if S003==716
replace Country="Spain" if S003==724
replace Country="Western Sahara" if S003==732
replace Country="Sudan" if S003==736
replace Country="Suriname" if S003==740
replace Country="Svalbard and Jan Mayen Islands" if S003==744
replace Country="Swaziland" if S003==748
replace Country="Sweden" if S003==752
replace Country="Switzerland" if S003==756
replace Country="Syrian Arab Republic" if S003==760
replace Country="Tajikistan" if S003==762
replace Country="Thailand" if S003==764
replace Country="Togo" if S003==768
replace Country="Tokelau" if S003==772
replace Country="Tonga" if S003==776
replace Country="Trinidad and Tobago" if S003==780
replace Country="United Arab Emirates" if S003==784
replace Country="Tunisia" if S003==788
replace Country="Turkey" if S003==792
replace Country="Turkmenistan" if S003==795
replace Country="Turks and Caicos Islands" if S003==796
replace Country="Tuvalu" if S003==798
replace Country="Uganda" if S003==800
replace Country="Ukraine" if S003==804
replace Country="Macedonia" if S003==807
replace Country="Egypt" if S003==818
replace Country="Great Britain" if S003==826
replace Country="Isle of Man" if S003==833
replace Country="Tanzania" if S003==834
replace Country="United States" if S003==840
replace Country="United States Virgin Islands" if S003==850
replace Country="Burkina Faso" if S003==854
replace Country="Uruguay" if S003==858
replace Country="Uzbekistan" if S003==860
replace Country="Venezuela" if S003==862
replace Country="Wallis and Futuna Islands" if S003==876
replace Country="Samoa" if S003==882
replace Country="Yemen" if S003==887
replace Country="Serbia and Montenegro" if S003==891
replace Country="Zambia" if S003==894
replace Country="Germany West" if S003==900
replace Country="Germany East" if S003==901
replace Country="Tambov" if S003==902
replace Country="Moscow" if S003==903
replace Country="Basque Country" if S003==904
replace Country="Andalusia" if S003==906
replace Country="Galicia" if S003==907
replace Country="Northern Ireland" if S003==909
replace Country="Valencia" if S003==910
replace Country="Serbia" if S003==911
replace Country="Montenegro" if S003==912
replace Country="SrpSka - Serbian Republic of Bosnia" if S003==913
replace Country="Bosnia Federation" if S003==914

tab Country

gen int Year=S020
tab Year
gen wt=S017

tab E110

recode E110 (1=4) (2=3) (3=2) (4=1), gen(SWD)

egen Country_Year=concat(Country Year), punct("_")
encode Country_Year, gen(Country_year_n)
tab Country_year_n, nolabel

replace wt=1 if wt==.

**************
gen m_SWD=.
gen sd_SWD=.
gen n_SWD=.
gen disat=.
gen disat4=.
gen maxsd=.
gen polar=.

svyset [pw=wt]

forvalues i=1/48 {
	quietly capture svy: mean SWD if Country_year_n ==`i'
	replace n_SWD=e(N) if Country_year_n==`i'
	quietly estat sd
	matrix mean_SWD`i'=r(mean)
	replace m_SWD=mean_SWD`i'[1,1] if Country_year_n ==`i'
	matrix sd_SWD`i'=r(sd)
	replace sd_SWD=sd_SWD`i'[1,1] if Country_year_n ==`i'
	replace disat4=m_SWD*-1+5 if Country_year_n ==`i'
	replace disat=disat4/4 if Country_year_n ==`i'
	replace maxsd=sqrt((1-m_SWD)*(m_SWD-4)*(n_SWD)/(n_SWD-1)) if Country_year_n ==`i'
	replace polar=sd_SWD/maxsd if Country_year_n ==`i'
	}
	
table Country_Year, c(mean m_SWD mean sd_SWD)
table Country, c(mean m_SWD mean sd_SWD)

table Country, c(mean disat mean polar)
gen Study="WVS"

collapse (first) Country Year Study (mean) n_SWD m_SWD sd_SWD disat polar   , by(Country_year)

*save WVS

***************************************************
***************************************************
*FROM THE ABOVE, WE CREATE THE POLARIZATION DATASET
***************************************************
*Use the names you gave the datasets above to append them*

*use "Afrobarom SWD.dta"
*append using "Asianbarom SWD.dta"
*append using "Eurobarom_trend SWD.dta"
*append using "Eurobarom post 2002.dta"
*append using "EVS SWD.dta"
*append using "LAPOP SWD.dta"
*append using "LatinoBarom SWD.dta"
*append using "WVS SWD.dta"

tab Country
replace Country="United Kingdom" if Country=="Great Britain"
replace Country="Russia" if Country=="Russian Federation"
tab Country

tab Year

drop  Country_Year

egen Country_Year=group(Country Year)

**averages

egen survey_count=count(polar), by(Country_Year)

gen wt=1/survey_count

drop if Country=="Cyprus (TCC)"
drop if Country=="Northern Ireland"
drop if Country=="Montenegro"
drop if Country=="Serbia"
drop if Country=="Serbia and Montenegro"
drop if Country=="Yugoslavia"
drop if  Country=="Kosovo"

drop if Year<1993 & Country=="Slovakia"

replace Country="Bosnia And Herzegovina" if Country=="Bosnia and Herzegovina"
replace Country="Croatia (Local Name: Hrvatska)" if Country=="Croatia"
replace Country="Iran " if Country=="Iran"
replace Country="Moldova, Republic Of" if Country=="Moldova"
replace Country="Cyprus" if Country=="Northern Cyprus"
replace Country="Russian Federation" if Country=="Russia"
replace Country="Slovakia" if Country=="Slovak Republic"
replace Country="Korea" if Country=="South Korea"
replace Country="Tanzania, United Republic Of" if Country=="Tanzania"
replace Country="Trinidad And Tobago" if Country=="Trinidad & Tobago"
replace Country="Viet Nam" if Country=="Vietnam"

drop if Year<1993 & Country=="Slovakia"


* save  Polarization dataset.dta

collapse  disat polar m_SWD sd_SWD (rawsum) n_SWD=n [aw=wt], by(Country Year)

egen Year4=cut(Year) , at(1971(4)2015)
egen disat4=mean(disat), by(Country Year4)
egen polar4=mean(polar), by(Country Year4)
egen m_SWD4=mean(disat), by(Country Year4)
egen sd_SWD4=mean(polar), by(Country Year4)

sort Country
merge Country using "ISO country codes.dta"
tab _merge
tab Country

replace Country="Croatia" if Country=="Croatia (Local Name: Hrvatska)"
replace Country="Iran" if Country=="Iran "
replace Country="Tanzania" if Country=="Tanzania, United Republic Of"

drop if _merge==2
drop _merge


* save  Polarization dataset.dta

*THIS IS THE DATA WE USE TO RUN THE MODELS*
